﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using IntoNails.Infrastructure.ViewModels;
using IntoNails.Infrastructure.Views;
using IntoNails.Models;
using IntoNails.Modules.CategoryManagement.Models;
using Microsoft.Practices.Prism.Commands;

namespace IntoNails.Modules.CategoryManagement.ViewModels
{
    public partial class CreateNewServicePopupViewModel : GenericInteractionViewModel<Service>, IHeaderInfoProvider<string>
    {
        #region Fields

        private const string ProductString = "Product";
        private const string SearchString = "Search";
        private const int DefaultMargin = 5;

        private string _headerInfo;

        private int _productLinkCount = 0;

        private Grid _productLinkGrid;

        private bool _isEditMode;

        #endregion

        #region Constructor(s)

        public CreateNewServicePopupViewModel()
        {
            this.AddProductLinkCommand = new DelegateCommand(this.OnAddProductLinkCommandExecuted);
            this.ProductLinkGridLoadedCallbackCommand = new DelegateCommand<Grid>(this.OnProductLinkGridLoaded);
        }

        #endregion

        #region Commands

        public ICommand AddProductLinkCommand { get; private set; }
        public ICommand ProductLinkGridLoadedCallbackCommand { get; private set; }

        #endregion

        #region Implementation of IHeaderInfoProvider<string>

        public string HeaderInfo
        {
            get { return this._headerInfo; }
            set
            {
                this._headerInfo = value;
                base.RaisePropertyChanged(() => HeaderInfo);
            }
        }

        #endregion

        #region Properties

        public bool IsEditMode
        {
            get { return this._isEditMode; }
            set
            {
                this._isEditMode = value;
                base.RaisePropertyChanged(() => IsEditMode);
            }
        }

        #endregion

        public override void ViewInvoked(object parameter)
        {
            base.ViewInvoked(parameter);

            ServiceDataContext serviceDataContext = parameter as ServiceDataContext;
            if (serviceDataContext == null)
                throw new ArgumentNullException("serviceDataContext");

            this.IsEditMode = serviceDataContext.IsEditMode;
            this.InteractionEntity.AssociatedCategory = this.SelectedCategory = serviceDataContext.CurrentCategory;
            this.CategoryCollection = serviceDataContext.CategoryCollection;
            this.HeaderInfo = this.IsEditMode ? "Edit Service" : "Add New Service";
        }

        #region Command Action Handlers

        private void OnProductLinkGridLoaded(Grid productLinkGrid)
        {
            this._productLinkGrid = productLinkGrid;

            // creates column definition for label (x3)
            ColumnDefinition idColumn = new ColumnDefinition
                                                  {
                                                      Width = new GridLength(0, GridUnitType.Auto)
                                                  };

            ColumnDefinition nameColumn = new ColumnDefinition
                                              {
                                                  Width = new GridLength(1, GridUnitType.Auto)
                                              };

            ColumnDefinition buttonColumn = new ColumnDefinition
                                                {
                                                    Width = new GridLength(0, GridUnitType.Auto)
                                                };

            // adds columns to grid
            this._productLinkGrid.ColumnDefinitions.Add(idColumn);
            this._productLinkGrid.ColumnDefinitions.Add(nameColumn);
            this._productLinkGrid.ColumnDefinitions.Add(buttonColumn);
            
            // adds first input row
            this.AddProductLinkEntry();
        }

        private void OnAddProductLinkCommandExecuted()
        {
            this.AddProductLinkEntry();
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Increases the row counter by 1, adds the entry count info to the dictionary.
        /// </summary>
        private void AddProductLinkEntry()
        {
            RowDefinition entryRowDefinition = new RowDefinition
                                                   {
                                                       Height = new GridLength(0, GridUnitType.Auto)
                                                   };

            this._productLinkGrid.RowDefinitions.Add(entryRowDefinition);

            Label entryNameLabel = new Label()
                                       {
                                           Content = string.Format("{0} {1}", ProductString, this._productLinkCount),
                                           Margin = new Thickness(DefaultMargin, DefaultMargin, DefaultMargin, DefaultMargin),
                                           Tag = this._productLinkCount
                                       };

            // creates text-boxes
            TextBox inputNameTextBox = new TextBox
                                           {
                                               Margin = new Thickness(DefaultMargin, DefaultMargin, DefaultMargin, DefaultMargin),
                                               Tag = this._productLinkCount,
                                               IsEnabled = false
                                           };
            //examNameTextBox.GotFocus += this.SmartMarkTextBoxOnGotFocus;

            Button searchButton = new Button()
                                      {
                                          Content = SearchString,
                                          Margin = new Thickness(DefaultMargin, DefaultMargin, DefaultMargin, DefaultMargin),
                                          Tag = this._productLinkCount
                                      };

            // set locations for text-boxes
            Grid.SetRow(entryNameLabel, this._productLinkCount);
            Grid.SetColumn(entryNameLabel, 0);

            Grid.SetRow(inputNameTextBox, this._productLinkCount);
            Grid.SetColumn(inputNameTextBox, 1);

            Grid.SetRow(searchButton, this._productLinkCount);
            Grid.SetColumn(searchButton, 2);

            // adds text-boxes to grid
            this._productLinkGrid.Children.Add(entryNameLabel);
            this._productLinkGrid.Children.Add(inputNameTextBox);
            this._productLinkGrid.Children.Add(searchButton);

            this._productLinkGrid.UpdateLayout();

            ++this._productLinkCount;

            // sets logical focus to examNameTextBox
            //examNameTextBox.Focus();

            // adds the entry count info to the dictionary
            //this._cellDictionary.Add(this._rowCount, 3); // this means this row has 3 columns at the beginning.
        }

        #endregion

    }
}
